use axum::Json;
use serde_json::Value;
use sea_orm::prelude::{Decimal, Json as SqlJson};

use crate::code;
// 获取 json
pub fn json_op(value:&Value, field:&str) -> Result<Option<SqlJson>, Json<Value>> { 
    if value.is_null() {
        Ok( None )
    }else {
        if value.is_object() || value.is_array() {
            Ok( Some(value.to_owned()) )
        }else{
            Err(code::err_code_msg_(501,field.to_string() + " 字段必须是 JSON 格式"))
        }
    }
}
pub fn json_arr_op(value:&Value, field:&str) -> Result<Option<SqlJson>, Json<Value>> { 
    if value.is_null() {
        Ok( None )
    }else {
        if value.is_array() {
            Ok( Some(value.to_owned()) )
        }else{
            Err(code::err_code_msg_(501,field.to_string() + " 字段必须是数组格式"))
        }
    }
}
pub fn json_obj_op(value:&Value, field:&str) -> Result<Option<SqlJson>, Json<Value>> { 
    if value.is_null() {
        Ok( None )
    }else {
        if value.is_object() {
            Ok( Some(value.to_owned()) )
        }else{
            Err(code::err_code_msg_(501,field.to_string() + " 字段必须是对象格式"))
        }
    }
}
// 将 json 转为数组
pub fn vec_op(value:&Value, field:&str) -> Result<Option<Vec<Value>>, Json<Value>> {
    if value.is_null() {
        Ok(None)
    }else{
        if let Some(f) = value.as_array() {
            Ok( Some(f.to_vec()) )
        }else{
            Err(code::err_code_msg_(501,field.to_string() + " 字段必须是数组"))
        }
    }
}
// 将已有数组值转为指定类型
pub fn vec_i64_json(value:&Value, field:&str) -> Result<Vec<i64>, Json<Value>> {
    match vec_i64_json_op(value, field)? {
        Some( f ) => Ok(f),
        None =>  {
            let str = if field.is_empty() { "必选参数".to_owned() } else { field.to_string() };
            Err(code::err_code_msg_(501,str + " 字段必填"))
        }
    }
}
pub fn vec_i32_json(value:&Value, field:&str) -> Result<Vec<i32>, Json<Value>> {
    match vec_i32_json_op(value, field)? {
        Some( f ) => Ok(f),
        None =>  {
            let str = if field.is_empty() { "必选参数".to_owned() } else { field.to_string() };
            Err(code::err_code_msg_(501,str + " 字段必填"))
        }
    }
}
pub fn vec_str_json(value:&Value, field:&str) -> Result<Vec<String>, Json<Value>> {
    match vec_str_json_op(value, field)? {
        Some( f ) => Ok(f),
        None =>  {
            let str = if field.is_empty() { "必选参数".to_owned() } else { field.to_string() };
            Err(code::err_code_msg_(501,str + " 字段必填"))
        }
    }
}
pub fn vec_i64_json_op(value:&Value, field:&str) -> Result<Option<Vec<i64>>, Json<Value>> {
    if value.is_null() {
        Ok(None)
    }else{
        if let Some(vec) = value.as_array() {
            let mut arr: Vec<i64> = Vec::new();
            let mut err = false;
            for v in vec {
                if let Some(f) = v.as_f64() {
                    arr.push(f as i64)
                }else{
                    err = true;
                    break;
                }
            }
            if err {
                Err(code::err_code_msg_(501,field.to_string() + " 字段无法转为整数数组"))
            }else{
                Ok( Some(arr) )
            }
        }else{
            Err(code::err_code_msg_(501,field.to_string() + " 必须是一维数字数组"))
        }
    }
}
pub fn vec_i32_json_op(value:&Value, field:&str) -> Result<Option<Vec<i32>>, Json<Value>> {
    if value.is_null() {
        Ok(None)
    }else{
        if let Some(vec) = value.as_array() {
            let mut arr: Vec<i32> = Vec::new();
            let mut err = false;
            for v in vec {
                if let Some(f) = v.as_f64() {
                    arr.push(f as i32)
                }else{
                    err = true;
                    break;
                }
            }
            if err {
                Err(code::err_code_msg_(501,field.to_string() + " 字段无法转为整数数组"))
            }else{
                Ok( Some(arr) )
            }
        }else{
            Err(code::err_code_msg_(501,field.to_string() + " 必须是一维数字数组"))
        }
    }
}
pub fn vec_u32_json_op(value:&Value, field:&str) -> Result<Option<Vec<u32>>, Json<Value>> {
    if value.is_null() {
        Ok(None)
    }else{
        if let Some(vec) = value.as_array() {
            let mut arr: Vec<u32> = Vec::new();
            let mut err = false;
            for v in vec {
                if let Some(f) = v.as_f64() {
                    arr.push(f as u32)
                }else{
                    err = true;
                    break;
                }
            }
            if err {
                Err(code::err_code_msg_(501,field.to_string() + " 字段无法转为整数数组"))
            }else{
                Ok( Some(arr) )
            }
        }else{
            Err(code::err_code_msg_(501,field.to_string() + " 必须是一维数字数组"))
        }
    }
}
pub fn vec_str_json_op(value:&Value, field:&str) -> Result<Option<Vec<String>>, Json<Value>> {
    if value.is_null() {
        Ok(None)
    }else{
        if let Some(vec) = value.as_array() {
            let mut arr: Vec<String> = Vec::new();
            let mut err = false;
            for v in vec {
                if let Some(f) = v.as_str() {
                    arr.push(f.to_owned())
                }else{
                    err = true;
                    break;
                }
            }
            if err {
                Err(code::err_code_msg_(501,field.to_string() + " 字段无法转为整数数组"))
            }else{
                Ok( Some(arr) )
            }
        }else{
            Err(code::err_code_msg_(501,field.to_string() + " 必须是一维数字数组"))
        }
    }
}
// 将 json 字符串转为指定数组类型
pub fn vec_i64_str_op(value:&Value, field:&str) -> Result<Option<Vec<i64>>, Json<Value>> {
    if value.is_null() {
        Ok(None)
    }else{
        if let Some(f) = value.as_str() {
            str_to_vec_i64_op(f)
        }else{
            Err(code::err_code_msg_(501,field.to_string() + " 字段必须是以 , 分隔的字符串"))
        }
    }
}
pub fn vec_i32_str_op(value:&Value, field:&str) -> Result<Option<Vec<i32>>, Json<Value>> {
    if value.is_null() {
        Ok(None)
    }else{
        if let Some(f) = value.as_str() {
            str_to_vec_i32_op(f)
        }else{
            Err(code::err_code_msg_(501,field.to_string() + " 字段必须是以 , 分隔的字符串"))
        }
    }
}
pub fn vec_u32_str_op(value:&Value, field:&str) -> Result<Option<Vec<u32>>, Json<Value>> {
    if value.is_null() {
        Ok(None)
    }else{
        if let Some(f) = value.as_str() {
            str_to_vec_u32_op(f)
        }else{
            Err(code::err_code_msg_(501,field.to_string() + " 字段必须是以 , 分隔的字符串"))
        }
    }
}
pub fn vec_str_str_op(value:&Value, field:&str) -> Result<Option<Vec<String>>, Json<Value>> {
    if value.is_null() {
        Ok(None)
    }else{
        if let Some(f) = value.as_str() {
            str_to_vec_str_op(f)
        }else{
            Err(code::err_code_msg_(501,field.to_string() + " 字段必须是以 , 分隔的字符串"))
        }
    }
}
// 将数字数组转为字符串数组
pub fn vec_str_vec_num_op(value:&Value, field:&str) -> Result<Option<Vec<String>>, Json<Value>> {
    if value.is_null() { Ok(None) }
    else if let Some(f) = value.as_array() {
        let mut arr: Vec<String> = Vec::new();
        for v in f {
            if let Some(k) = v.as_f64() {
                arr.push(k.to_string())
            }
        }
        Ok(Some(arr))
    }else {
        Err(code::err_code_msg_(501,field.to_string() + " 字段必须是数字数组"))
    }
}
// 将字符串转为指定数组类型
pub fn vec_i64_str_(value:&str, field:&str) -> Result<Vec<i64>, Json<Value>> {
    match str_to_vec_i64_op(value)? {
        Some( f ) => Ok(f),
        None =>  {
            let str = if field.is_empty() { "必选参数".to_owned() } else { field.to_string() };
            Err(code::err_code_msg_(501,str + " 字段必填"))
        }
    }
}
pub fn vec_i32_str_(value:&str, field:&str) -> Result<Vec<i32>, Json<Value>> {
    match str_to_vec_i32_op(value)? {
        Some( f ) => Ok(f),
        None =>  {
            let str = if field.is_empty() { "必选参数".to_owned() } else { field.to_string() };
            Err(code::err_code_msg_(501,str + " 字段必填"))
        }
    }
}
pub fn vec_u32_str_(value:&str, field:&str) -> Result<Vec<u32>, Json<Value>> {
    match str_to_vec_u32_op(value)? {
        Some( f ) => Ok(f),
        None =>  {
            let str = if field.is_empty() { "必选参数".to_owned() } else { field.to_string() };
            Err(code::err_code_msg_(501,str + " 字段必填"))
        }
    }
}
pub fn str_to_vec_str(value:&str, field:&str) -> Result<Vec<String>, Json<Value>> {
    match str_to_vec_str_op(value)? {
        Some( f ) => Ok(f),
        None =>  {
            let str = if field.is_empty() { "必选参数".to_owned() } else { field.to_string() };
            Err(code::err_code_msg_(501,str + " 字段必填"))
        }
    }
}
pub fn str_to_vec_i64_op(value:&str) -> Result<Option<Vec<i64>>, Json<Value>> {
    if value.is_empty() {
        Ok(None)
    }else{
        let rs = value.split(",").collect::<Vec<&str>>();
        let mut arr: Vec<i64> = Vec::new();
        let mut err = false;
        for v in rs {
            if let Ok(f) = v.parse::<i64>() {
                arr.push(f)
            }else{
                err = true;
                break
            }
        }
        if err {
            Ok(None)
        }else{
            if arr.is_empty() { Ok(None) } else { Ok( Some(arr) ) }
        }
    }
}
pub fn str_to_vec_i32_op(value:&str) -> Result<Option<Vec<i32>>, Json<Value>> {
    if value.is_empty() {
        Ok(None)
    }else{
        let rs = value.split(",").collect::<Vec<&str>>();
        let mut arr: Vec<i32> = Vec::new();
        let mut err = false;
        for v in rs {
            if let Ok(f) = v.parse::<i32>() {
                arr.push(f)
            }else{
                err = true;
                break
            }
        }
        if err {
            Ok(None)
        }else{
            if arr.is_empty() { Ok(None) } else { Ok( Some(arr) ) }
        }
    }
}
pub fn str_to_vec_u32_op(value:&str) -> Result<Option<Vec<u32>>, Json<Value>> {
    if value.is_empty() {
        Ok(None)
    }else{
        let rs = value.split(",").collect::<Vec<&str>>();
        let mut arr: Vec<u32> = Vec::new();
        let mut err = false;
        for v in rs {
            if let Ok(f) = v.parse::<u32>() {
                arr.push(f)
            }else{
                err = true;
                break
            }
        }
        if err {
            Ok(None)
        }else{
            if arr.is_empty() { Ok(None) } else { Ok( Some(arr) ) }
        }
    }
}
pub fn str_to_vec_str_op(value:&str) -> Result<Option<Vec<String>>, Json<Value>> {
    if value.is_empty() {
        Ok(None)
    }else{
        let rs = value.split(",").collect::<Vec<&str>>();
        let mut arr: Vec<String> = Vec::new();
        let mut err = false;
        for v in rs {
            let f = v.parse::<String>().unwrap_or_default();
            if f.is_empty() {
                err = true;
                break
            }else {
                arr.push(f);
            } 
        }
        if err {
            Ok(None)
        }else{
            if arr.is_empty() { Ok(None) } else { Ok( Some(arr) ) }
        }
    }
}
// vec_num_to_vec_str
pub fn vecstr_i32(vec:Vec<i32>) -> Result<Vec<String>, Json<Value>> {
    let mut arr: Vec<String> = Vec::new();
    for v in vec {
        arr.push(v.to_string())
    }
    Ok( arr )
}
pub fn vecstr_u32(vec:Vec<u32>) -> Result<Vec<String>, Json<Value>> {
    let mut arr: Vec<String> = Vec::new();
    for v in vec {
        arr.push(v.to_string())
    }
    Ok( arr )
}
// dec
pub fn dec_i32(num: i32) -> Decimal { Decimal::from(num) }
pub fn i32_dec_op(dec: Decimal) -> Option<i32> { 
    let str = dec.to_string();
    match str.parse::<i32>() {
        Ok(f) => Some(f),
        Err(_) => None
    }
}
pub fn f32_dec_op(dec: Decimal) -> Option<f32>  { 
    let str = dec.to_string();
    match str.parse::<f32>() {
        Ok(f) => Some(f),
        Err(_) => None
    }
}